/*
 * Adobe Camera Raw Calibrator Tools
 * AcrCalibrator-AlignmentCheck
 * Version: x$((ver))
 * http://chromoholics.com/
 *
 * Copyright (c) 2007 Thomas Fors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// =======================================================
// Configuration
// =======================================================

var scriptVersion = "x$((ver))";

#include "Acr.js"
#include "ColorChecker.js"
#include "ProPhotoRGBColor.js"
#include "Matrix3x3.js"
#include "Vector1x3.js"

// -------------------------------------------------------
// MarkPatch()
// -------------------------------------------------------
function MarkPatch(n) {
	var c = new LabColor();
	c.l = (cc.refColor[n].L + 50) % 100;
	c.a = 0;
	c.b = 0;
	try {
		cc.ReadPatch(n);
	} catch (e) {
	}
	activeDocument.selection.fill(c);
}

// =======================================================
// Main Code
// =======================================================

// Save user's preferences
var rulerUnits = preferences.rulerUnits;
var nonLinearHistory = preferences.nonLinearHistory;
var numberOfHistoryStates = preferences.numberOfHistoryStates;

try {
	// Ask to change any required preferences
	var msg = ""
	if ( rulerUnits != Units.PIXELS )
		msg += "   --> Ruler Units must be set to Pixels.\n";
	if ( nonLinearHistory )
		msg += "   --> Nonlinear History must be disabled.\n";
	if ( numberOfHistoryStates < 2 )
		msg += "   --> History States must be set to 2 or more.\n";
	
	if ( msg != "" ) {
		if (confirm("This script needs to change your Photoshop preferences:\n\n" + msg + "\n" +
					"Select YES to allow this script to change your Photoshop preferences.\n" +
					"Present settings will be restored when the script exits.\n\n" +
					"Select NO to leave your preferences unchanged and exit ACR Calibrator.\n\n" +
					"Warning: if this script crashes unexpectedly, you will need to restore your\n" +
					"preferences manually."))
		{
			preferences.rulerUnits = Units.PIXELS;
			preferences.nonLinearHistory = false;
			if ( numberOfHistoryStates < 2 )
				preferences.numberOfHistoryStates = 2;
		} else {
			throw ("User aborted.");
		}
	}
	
	// Initialize ACR
	var acr = new Acr();
	acr.Initialize();
	
	// Initialize ColorChecker
	var cc = new ColorChecker();
	cc.FindPatchesFromActiveDoc();

	// Hide all layers except background
	for (i=0; i<activeDocument.artLayers.length; i++) {
		if ( ! activeDocument.artLayers[i].isBackgroundLayer) {
			activeDocument.artLayers[i].visible = false;
		}
	}
	
	// Add a layer and mark the patches
	activeDocument.activeLayer = activeDocument.artLayers.add();
	for (i=0; i<24; i++) {
		MarkPatch(i);
	}
	activeDocument.selection.deselect();
	
	// Restore user's ruler units
	preferences.rulerUnits = rulerUnits;
	preferences.nonLinearHistory = nonLinearHistory;
	preferences.numberOfHistoryStates = numberOfHistoryStates;
	
} catch (e) {
	
	// Restore user's ruler units
	preferences.rulerUnits = rulerUnits;
	preferences.nonLinearHistory = nonLinearHistory;
	preferences.numberOfHistoryStates = numberOfHistoryStates;
	
	alert(e);
}
